<?php

class Model_UsersMapper
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Model_DbTable_Users');
        }
        return $this->_dbTable;
    }
    
	//find the user with user account
	public function findUserByAccount($account, Model_Users $users) {
		
		$select = $this->getDbTable()->select()->from('users')->where('account=?',$account);
		
		$row = $this->getDbTable()->fetchRow($select);
		
		if (0 == count($row)) {
            return;
        }
		
		$users->setUser_id($row->user_id);
		$users->setAccount($row->account);
		$users->setFb_account($row->fb_account);
		$users->setName($row->name);
		$users->setPassword($row->password);
		$users->setUser_type($row->user_type);
		$users->setAddress($row->address);
		$users->setBirthday($row->birthday);
		$users->setTel($row->tel);
		$users->setUniversity($row->university);
		$users->setMajor($row->major);
		
	}

    // select user with user id
    public function find($userId, Model_Users $users)
    {
        $result = $this->getDbTable()->find($userId);
        
		if (0 == count($result)) {
            return;
        }
        $row = $result->current();
		$users->setUser_id($row->user_id);
		$users->setAccount($row->account);
		$users->setFb_account($row->fb_account);
		$users->setName($row->name);
		$users->setPassword($row->password);
		$users->setUser_type($row->user_type);
		$users->setAddress($row->address);
		$users->setBirthday($row->birthday);
		$users->setTel($row->tel);
		$users->setUniversity($row->university);
		$users->setMajor($row->major);
    }
	
    // find user with the letter input from search bar
    public function findByPartial($phrase,$return_num)
    {
        $select = $this->getDbTable()->select();
        $select->from('users')
                ->where('name LIKE ?',"%$phrase%")
                ->order(array('name ASC'));
//        echo $select->__toString()."\n";
        $results = $this->getDbTable()->fetchAll($select);
        $entries=array();
        foreach ($results as $row){
            $row = $results->current();
            $users = new Model_Users();
            $users->setUser_id($row->user_id);
			$users->setAccount($row->account);
			$users->setFb_account($row->fb_account);
			$users->setName($row->name);
			$users->setPassword($row->password);
			$users->setUser_type($row->user_type);
			$users->setAddress($row->address);
			$users->setBirthday($row->birthday);
			$users->setTel($row->tel);
			$users->setUniversity($row->university);
			$users->setMajor($row->major);
			$entries[] = $users;
        }
        return $entries;
    }
}

